package com.zhuang.arraylist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Spliterator;

/**
 * 
 * @ClassName: ArrayListDemo
 * @Description: 补充ArrayList重要的方法
 * @author: KangXiaoZhuang
 * @date: 2022年10月8日 下午9:06:05
 */
@SuppressWarnings("all")
public class ArrayListDemo extends ArrayList<Integer> {

	public static void main(String[] args) {
		testRetainAll();
		testRemoveRange();
		testForEach();
		testSpliterator();
		testBinarySearch();
	}

	/**
	 * 
	 * @Title: testRetainAll
	 * @Description: RetainAll的方法使用
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public static void testRetainAll() {
		ArrayList<String> list1 = new ArrayList<String>();
		list1.add("pen");
		list1.add("pencil");
		list1.add("paper");
		ArrayList<String> list2 = new ArrayList<String>();
		list2.add("pen");
		list2.add("paper");
		list2.add("books");
		list2.add("rubber");
		System.out.println("list1 Contains :" + list1);
		System.out.println("list2 Contains :" + list2);

		list2.retainAll(list1);
		System.out.println("\nAfter Applying list2.retainAll(list1) \n");

		System.out.println("list1 Contains :" + list1);
		System.out.println("list2 Contains :" + list2);
	}

	/**
	 * 
	 * @Title: testRemoveRange
	 * @Description: RemoveRange()方法使用
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public static void testRemoveRange() {
		ArrayListDemo list = new ArrayListDemo();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(12);
		list.add(9);
		list.add(13);

		System.out.println("The list before using removeRange:" + list);
		list.removeRange(0, 2);
		System.out.println("The list after using removeRange:" + list);

	}

	/**
	 * 
	 * @Title: testForEach
	 * @Description: forEach()方法的使用
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public static void testForEach() {
		ArrayList<String> students = new ArrayList<String>();

		students.add("Ram");
		students.add("Mohan");
		students.add("Sohan");
		students.add("Rabi");

		students.forEach((n) -> print(n));
		students.forEach((n) -> System.out.println(n));
		students.forEach(System.out::println);
	}

	public static void print(String n) {
		System.out.println(n);
	}

	/**
	 * 
	 * @Title: testSpliterator
	 * @Description: spliterator()方法使用
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public static void testSpliterator() {
		ArrayList<String> list = new ArrayList<String>();

		list.add("abc@geeksforgeeks.org");
		list.add("user@geeksforgeeks.org");
		list.add("pqr@geeksforgeeks.org");
		list.add("random@geeksforgeeks.org");
		list.add("randomuser@geeksforgeeks.org");

		Spliterator<String> emails = list.spliterator();

		System.out.println("list of Emails:");

		emails.forEachRemaining((n) -> System.out.println(n));
	}

	/**
	 * 
	 * @Title: testBinarySearch
	 * @Description: binarySearch方法
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public static void testBinarySearch() {
		// 数组初始化
		byte byteArr[] = { 10, 20, 15, 22, 35 };
		char charArr[] = { 'g', 'p', 'q', 'c', 'i' };
		int intArr[] = { 10, 20, 15, 22, 35 };
		double doubleArr[] = { 10.2, 15.1, 2.2, 3.5 };
		float floatArr[] = { 10.2f, 15.1f, 2.2f, 3.5f };
		short shortArr[] = { 10, 20, 15, 22, 35 };

		// 先进行排序
		Arrays.sort(byteArr);
		Arrays.sort(charArr);
		Arrays.sort(intArr);
		Arrays.sort(doubleArr);
		Arrays.sort(floatArr);
		Arrays.sort(shortArr);

		// 目标值
		byte byteKey = 35;
		char charKey = 'g';
		int intKey = 22;
		double doubleKey = 1.5;
		float floatKey = 35;
		short shortKey = 5;

		System.out.println(byteKey + " found at index = " + Arrays.binarySearch(byteArr, byteKey));
		System.out.println(charKey + " found at index = " + Arrays.binarySearch(charArr, charKey));
		System.out.println(intKey + " found at index = " + Arrays.binarySearch(intArr, intKey));
		System.out.println(doubleKey + " found at index = " + Arrays.binarySearch(doubleArr, doubleKey));
		System.out.println(floatKey + " found at index = " + Arrays.binarySearch(floatArr, floatKey));
		System.out.println(shortKey + " found at index = " + Arrays.binarySearch(shortArr, shortKey));
	}

}
